﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CS_Util {
    public static class C_String {
        public const char e_split01 = '‡';
        public const char e_split02 = '➜';
        public const char e_split03 = '✪';
        public const char e_split04 = '┃';
        public const char e_split05 = '⊙';
        public const char e_split06 = '◉';
        public const char e_split07 = '█';
        public const char e_split08 = '◆';
        public const char e_split09 = '▲';
        public const char e_split10 = '▼';
        public const char e_split11 = '✖';
        public static string S_消除中文字符(string ss, int zLimit = 60, bool isToLower = true) {
            StringBuilder ssb = new StringBuilder();
            for (int i = 0; i < ss.Length; i++) {
                int ii = (int)ss[i];
                if (ii > 127) {
                    ssb.Append(ii);
                } else {
                    ssb.Append(ss[i]);
                }
            }
            ss = ssb.ToString();
            ss = ss.Replace("\\", "/");
            if (isToLower) {
                ss = ss.ToLower();
            }
            string[] sss = ss.Split('/');
            if (sss.Length > 1) {
                string ssName = Path.GetFileName(ss);
                if (ssName.Length > zLimit) {
                    ss = ss.Remove(ss.Length - ssName.Length, ssName.Length);
                    ssName = ssName.Remove(0, ssName.Length - zLimit);
                    //string sss4 = Path.GetDirectoryName(ss);
                    if (ssName[0] == '.') {
                        ssName = "a_" + ssName;
                    }
                    ss = ss + ssName;
                    C_Log.LogError(ssName + "_长度超标__已经被限制为60______" + ss);
                }
            } else {
                if (ss.Length > zLimit) {
                    ss = ss.Remove(0, ss.Length - zLimit);
                    if (ss[0] == '.') {
                        ss = "a_" + ss;
                    }
                    C_Log.LogError("长度超标__" + ss);
                }
            }
            return ss;
        }

        public static string S_Fix中文(string ss, int zLimit = 60) {
            StringBuilder ssb = new StringBuilder();
            for (int i = 0; i < ss.Length; i++) {
                int ii = (int)ss[i];
                if (ii > 127) {
                    ssb.Append(ii);
                } else {
                    ssb.Append(ss[i]);
                }
            }
            string ss2 = ssb.ToString();
            int ii3 = (int)ss2[0];
            //if(65> ii3 && ii3 < 90  || 97 > ii3 && ii3 < 122 || ss2[0]=='_') {
            //    return ss2;
            //}
            if (16 > ii3 || ii3 > 25) {
                return ss2;
            }
            return "a_"+ss2;
        }
        public static string S_EscapeURL(string ss0, int zLimit = 60, bool isToLower = true) {
            StringBuilder ssb = new StringBuilder();
            for (int i = 0; i < ss0.Length; i++) {
                int ii = (int)ss0[i];
                if (ii > 127) {
                    ssb.Append(ii);
                } else {
                    ssb.Append(ss0[i]);
                }
            }
            string ss = ssb.ToString();
            ss = ss.Replace("\\", "/");
            if (isToLower) {
                ss = ss.ToLower();
            }
            //ss = WWW.EscapeURL(ss);
            string[] sss = ss.Split('/');
            if (sss.Length > 1) {
                string ssName = Path.GetFileName(ss);
                if (ssName.Length > zLimit) {
                    string directory = ss.Remove(ss.Length - ssName.Length, ssName.Length);
                    ssName = ssName.Remove(0, ssName.Length - zLimit);

                    ss = directory + ssName;
                    C_Log.LogError(ss0 + "_长度超标__已经被限制为60______" + ss);
                }
                //ss = "";
                //for(int i = 0; i < sss.Length-1; i++) {
                //    ss += WWW.EscapeURL(sss[i]) + "/";
                //}
                //ss += WWW.EscapeURL(ssName);
            } else {
                if (ss.Length > zLimit) {
                    ss = ss.Remove(0, ss.Length - zLimit);
                    C_Log.LogError("长度超标__" + ss);
                }
                //ss = WWW.EscapeURL(ss);
            }
            ss = ss.Replace(".", "-");
            ss = ss.Replace("@", "1");
            ss = ss.Replace("#", "2");
            ss = ss.Replace("%", "3");
            ss = ss.Replace("?", "4");
            ss = ss.Replace("&", "5");
            ss = ss.Replace("!", "6");
            ss = ss.Replace("+", "7");
            return ss;
        }
        public static int S_StringLength(string ss) {
            int length = 0;
            for (int i = 0; i < ss.Length; i++) {
                int ii = (int)ss[i];
                if (ii > 127) {
                    length += 2;
                } else {
                    length++;
                }
            }
            return length;
        }
    }
    public static class C_Extend_string0 {


        /// <summary>
        /// 字符串,拆分模块---拆除前后括号--{--}-----------
        /// </summary>
        public static string[] S_SplitModule(this string content, string startString, string endString) {
            string[] sss = new string[3];
            int startPosi = content.IndexOf(startString);
            int endPosi = startPosi;
            int index = startPosi;
            int layer = -1;
            while (layer < 0) {
                int startPosi0 = content.IndexOf(startString, index + 1);
                int endPosi0 = content.IndexOf(endString, index + 1);
                if (startPosi0 < 0) {
                    startPosi0 = endPosi0 + 1;
                }
                if (endPosi0 <= 0) {
                    C_Log.LogErrorFormat("超级错误.sssssssssssssss");
                }
                if (startPosi0 < endPosi0) {
                    index = startPosi0;
                    layer--;
                } else {
                    index = endPosi0;
                    endPosi = index;
                    layer++;
                }
            }

            sss[0] = content.Substring(0, startPosi);
            sss[1] = content.Substring(startPosi + 1, endPosi - startPosi - 1);
            sss[2] = content.Substring(endPosi + 1, content.Length - endPosi - 1);
            return sss;
        }



         /// <summary>
         /// 目录名显示长度限制
         /// </summary>
        public static string S_DirectoryNameLengthLimit(this string directoryName, int lengthLimit) {
            if (directoryName.Length > lengthLimit) {
                int length0 = lengthLimit / 2;
                string start = directoryName.Substring(0, length0);
                string end = directoryName.Substring(directoryName.Length- length0, length0);
                return string.Format("{0}-******-{1}", start, end);
            }
            return directoryName;
        }




        /// <summary>
        /// 字符串是否包含通讯禁止字符-------------
        /// </summary>
        public static bool S_是否合格(this string ss) {
            if (ss.Contains("#9")) {
                return false;
            }
            if (ss.Contains("#8")) {
                return false;
            }
            if (ss.Contains("#7")) {
                return false;
            }
            if (ss.Contains("#6")) {
                return false;
            }
            if (ss.Contains("#5")) {
                return false;
            }
            if (ss.Contains("#4")) {
                return false;
            }
            if (ss.Contains("#3")) {
                return false;
            }
            if (ss.Contains("#2")) {
                return false;
            }
            if (ss.Contains("#1")) {
                return false;
            }
            if (ss.Contains("#0")) {
                return false;
            }
            if (ss.Contains("@")) {
                return false;
            }
            if (ss.Contains("&")) {
                return false;
            }
            if (ss.Contains("_")) {
                return false;
            }
            if (ss.Contains("*")) {
                return false;
            }
            if (ss.Contains(".")) {
                return false;
            }
            if (ss.Contains(",")) {
                return false;
            }
            if (ss.Contains("-")) {
                return false;
            }
            if (ss.Contains(":")) {
                return false;
            }
            return true;
        }
        public static string S_GetMD5(this string ss) {
            byte[] bytes = Encoding.UTF8.GetBytes(ss);
            return S_GetMD5(bytes);
        }
        public static string S_GetMD5(this byte[] bytes) {
            using (MD5 m5 = MD5.Create()) {
                bytes = m5.ComputeHash(bytes);
                m5.Clear();
                StringBuilder ssb = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++) {
                    ssb.Append(bytes[i].ToString("x2"));
                }
                return ssb.ToString();
            }
        }

        public static string S_HashToMD5Hex(this string ss) {
            byte[] bytes = Encoding.UTF8.GetBytes(ss);
            return S_HashToMD5Hex(  bytes);
        }

        public static string S_HashToMD5Hex(this byte[] bytes) {
            using (MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider()) {
                bytes = m5.ComputeHash(bytes);
                StringBuilder ssb = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++) {
                    ssb.Append(bytes[i].ToString("x2"));
                }
                m5.Clear();
                return ssb.ToString();
            }
        }

        public static string S_SetColor(this string ss, string colorText, int fontSize) {
            return string.Format("<size={0}>{1}</size>", fontSize, ss.S_SetColor(colorText));//<size=50>www</size>  
        }
        /// <summary>
        /// z颜色='ffff00'
        /// </summary>
        /// <param name="colorText='ffff00'"></param>
        public static string S_SetColor(this string ss, string colorText) {
            //return string.Format("<color=#{0}>{1}</color>", colorText, ss);
            int index = ss.IndexOf('\n');
            if (index > 0) {
                int index2 = ss.IndexOf('\n', index+1);
                if (index2 > index) {
                    //ss = ss.Insert(index2 + 1, string.Format(" <color=#{0}> ", colorText));
                    ss = ss.Insert(index2, " </color> ");
                    ss = string.Format("<color=#{0}> {1}", colorText, ss);
                } else {
                    index = -1;
                }
                //ss = ss.Replace("\n", string.Format("</color>\n<color=#{0}>", colorText));
                //ss = ss.Insert(index, string.Format("</color>\n\n<color=#{0}>", colorText));
            } else {
                ss = string.Format("<color=#{0}>{1}</color>", colorText, ss);
            }
            //string ss2 =  string.Format("<color=#{0}>{1}</color>", colorText, ss);
            return ss;
        }
        public static string S_SetColor(this string ss, int fontSize) {
            return string.Format("<size={0}>{1}</size>", fontSize, ss);//<size=50>www</size>  
        }
        public static string S_SetLogColor(this string ss, string colorText) {
            //return string.Format("<font size='{0}' color='#{1}'>{2}</font>", fontSize, colorText, ss);//<size=50>www</size>  
            return string.Format("{0}#{1}#", ss, colorText);
        }
        public static string S_GetLogColor(this string log, out string colorText) {
            if (log != null && log.Length > 8 && log[log.Length-1] == '#' && log[log.Length - 8] == '#') {
                colorText = log.Substring(log.Length - 7, 6);
                log = log.Substring(0, log.Length - 8);
                return log;
            }
            colorText = null;
            return log;
        }
        /// <summary>
        /// 两位数时间 2天21小时
        /// </summary>
        public static string S_TimeToString(this float v秒) {
            return S_TimeToString((int)v秒);
        }
        /// <summary>
        /// 两位数时间 2天21小时
        /// </summary>
        public static string S_TimeToString(this int z秒) {
            string ss = "";
            int z天 = z秒 / 86400;
            int z时 = (z秒 % 86400) / 3600;
            int z分 = (z秒 % 3600) / 60;
            int z秒t = (z秒 % 60);
            if (z天 > 0) {
                //ss = "<color=#00ff00>" + z天 + "</color>d";
                ss += string.Format("<color=#{0}>{1}</color>d", textColor, z天);
                if (z时 > 0) {
                    //ss += "<color=#00ff00>" + z时 + "</color>h";
                    ss += string.Format("<color=#{0}>{1}</color>h", textColor, z时);
                }
            } else {
                if (z时 > 0) {
                    //ss += "<color=#00ff00>" + z时 + "</color>h";
                    ss += string.Format("<color=#{0}>{1}</color>h", textColor, z时);
                    if (z分 > 0) {
                        //ss += "<color=#00ff00>" + z分 + "</color>m";
                        ss += string.Format("<color=#{0}>{1}</color>m", textColor, z分);
                    }
                } else {
                    if (z分 > 0) {
                        //ss += "<color=#00ff00>" + z分 + "</color>m";
                        ss += string.Format("<color=#{0}>{1}</color>m", textColor, z分);
                        if (z秒t > 0) {
                            //ss += "<color=#00ff00>" + z秒t + "</color>s";
                            ss += string.Format("<color=#{0}>{1}</color>s", textColor, z秒t);
                        }
                    } else {
                        if (z秒t > 0) {
                            ss += string.Format("<color=#{0}>{1}</color>s", textColor, z秒t);
                        } else {
                            ss += string.Format("<color=#{0}>{1}</color>s", textColor, 0);
                        }
                    }
                }
                //string.Format("ddss{0},ds{1}s","dd",45);
                //string.Format("<color=#00ff00>{0}</color>{1}",20,45);
            }
            return ss;
        }
        public static string textColor = "00ff00";
        public static string S_NumberToString(this float value, string format = "0") {
            if (value < 0) {
                return value.ToString("0.000");
            } else if (value < 10) {
                return value.ToString("0.00");
            } else if (value < 100) {
                return value.ToString("0.0");
            } else {
                return S_NumberToString((double)value, format);
            }
        }
        public static string S_NumberToString(this int value, string format = "0") {
            return S_NumberToString((double)value);
        }
        public static string S_NumberToString(this long value, string format = "0") {
            return S_NumberToString((double)value);
        }
        /// <summary>
        /// K,M,G,T,P,E,B
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string S_NumberToString(this double value, string format = "0") {
            if (value < 1000) {
                return value.ToString(format);
            }
            string ss = value.ToString("0");
            int z缩进位数 = ss.Length / 3;
            int z缩进标准长度 = z缩进位数 * 3;
            int z多余长度 = ss.Length - z缩进标准长度;

            switch (z多余长度) {
                case 0:
                    return ss.Substring(0, 3) + GetVs[z缩进位数 - 2];
                case 1:
                    return ss.Substring(0, 3).Insert(1, ".") + GetVs[z缩进位数 - 1];
                case 2:
                    return ss.Substring(0, 3).Insert(2, ".") + GetVs[z缩进位数 - 1];
            }
            return "rrro";
        }
        static char[] GetVs = new char[] { 'K', 'M', 'G', 'T', 'P', 'E', 'B', 'O', 'W', 'Y', 'X', 'U', 'N' };
        //public static string ToStringLog(this float[][] array, string format) {
        //    if (array == null|| array.Length==0) {
        //        return "null";
        //    }
        //    StringBuilder ssb = new StringBuilder();
        //    for (int i = 0; i < array.Length; i++) {
        //        for (int ii = 0; ii < array[i].Length; ii++) {
        //            ssb.AppendFormat("{0},", array[i][ii].ToString(format));
        //        }
        //        ssb.Append("+");
        //    }
        //    string ss = ssb.ToString();
        //    if (array.Length > 1) {
        //        ss = ss.Substring(0, ss.Length - 1);
        //    }
        //    return ss;
        //}
        //public static string ToStringLog(this float[] array, string format) {
        //    if (array == null || array.Length == 0) {
        //        return "null";
        //    }
        //    StringBuilder ssb = new StringBuilder();
        //    for (int i = 0; i < array.Length; i++) {
        //        ssb.AppendFormat("{0},", array[i].ToString(format));
        //    }
        //    string ss = ssb.ToString();
        //    if (array.Length > 1) {
        //        ss = ss.Substring(0, ss.Length - 1);
        //    }
        //    return ss;
        //}
        public static string ToStringLog<T>(this T[] array, Func<T, object> d_GetObj = null) {
            if (array == null || array.Length == 0) {
                return "null";
            }
            StringBuilder ssb = new StringBuilder();
            for (int ii = 0; ii < array.Length; ii++) {
                object obj = array[ii];
                if (d_GetObj != null) {
                    obj = d_GetObj(array[ii]);
                }
                if (ii == 0) {
                    ssb.AppendFormat("{0}", obj);
                } else {
                    ssb.AppendFormat(",_{0}", obj);
                }
            }
            return ssb.ToString();
        }
        public static string ToStringLog<T>(this T[][] array, Func<T, object> d_GetObj = null) {
            if (array == null || array.Length == 0) {
                return "null";
            }
            StringBuilder ssb = new StringBuilder();
            for (int i = 0; i < array.Length; i++) {
                if (i != 0) {
                    ssb.Append("+");
                }
                for (int ii = 0; ii < array[i].Length; ii++) {
                    object obj = array[i][ii];
                    if (d_GetObj != null) {
                        obj = d_GetObj(array[i][ii]);
                    }
                    if (ii == 0) {
                        ssb.AppendFormat("{0}", obj);
                    } else {
                        ssb.AppendFormat(",_{0}", obj);
                    }
                }
            }
            string ss = ssb.ToString();
            if (array.Length > 1) {
                ss = ss.Substring(0, ss.Length - 1);
            }
            return ss;
        }
        public static string ToStringLog<T>(this List<T> list, Func<T, object> d_GetObj = null) {
            if (list == null || list.Count == 0) {
                return "null";
            }
            StringBuilder ssb = new StringBuilder();
            for (int ii = 0; ii < list.Count; ii++) {
                object obj = list[ii];
                if (d_GetObj != null) {
                    obj = d_GetObj(list[ii]);
                }
                if (ii == 0) {
                    ssb.AppendFormat("{0}", obj);
                } else {
                    ssb.AppendFormat(",_{0}", obj);
                }
            }
            return ssb.ToString();
        }
        public static string ToStringLog<T>(this List<List<T>> list, Func<T, object> d_GetObj = null) {
            if (list == null || list.Count == 0) {
                return "null";
            }
            StringBuilder ssb = new StringBuilder();
            for (int i = 0; i < list.Count; i++) {
                if (i != 0) {
                    ssb.Append("+");
                }
                for (int ii = 0; ii < list[i].Count; ii++) {
                    object obj = list[i][ii];
                    if (d_GetObj != null) {
                        obj = d_GetObj(list[i][ii]);
                    }
                    if (ii == 0) {
                        ssb.AppendFormat("{0}", obj);
                    } else {
                        ssb.AppendFormat(",_{0}", obj);
                    }
                }
            }
            string ss = ssb.ToString();
            if (list.Count > 1) {
                ss = ss.Substring(0, ss.Length - 1);
            }
            return ss;
        }
        public static string ToStringLog<T, V>(this Dictionary<T, V> dic0, Func<V, object> d_GetObj = null) {
            if (dic0 == null || dic0.Count == 0) {
                return "null";
            }
            Type typeV = typeof(V);
            bool isClass = typeV.IsClass;
            StringBuilder ssb = new StringBuilder();
            int ii = 0;
            var dic = dic0.GetEnumerator();
            while (dic.MoveNext()) {
                object value = 0;
                if (d_GetObj != null) {
                    value = d_GetObj(dic.Current.Value);
                } else {
                    if (isClass) {
                        if (value == null) {
                            value = "null";
                        } else {
                            value = "obj";
                        }
                    } else {
                        value = dic.Current.Value;
                    }
                }
                if (ii == 0) {
                    ssb.AppendFormat("{0}={1}", dic.Current.Key, value);
                } else {
                    ssb.AppendFormat(",_{0}={1}", dic.Current.Key, value);
                }
                ii++;
            }
            return ssb.ToString();
        }
        public static string S_10进制到62进制(this Int64 z数字) {
            int z进制 = 62;
            byte[] bb = new byte[20];
            int i = 0;
            while (z数字 != 0) {
                i++;
                bb[bb.Length - i] = byte.Parse((z数字 % z进制).ToString());
                z数字 /= z进制;
            }
            //0=0,A=36,a=10
            //0=48,A=65,a=97
            string ss = "";
            for (int i2 = bb.Length - i; i2 < bb.Length; i2++) {
                ss += " " + bb[i2];
                if (bb[i2] < 10) {
                    bb[i2] = byte.Parse((bb[i2] + 48).ToString());
                } else if (bb[i2] < 36) {
                    bb[i2] = byte.Parse((bb[i2] + 87).ToString());
                } else {
                    bb[i2] = byte.Parse((bb[i2] + 29).ToString());
                }
            }
            return System.Text.Encoding.UTF8.GetString(bb, bb.Length - i, i);
        }
        public static Int64 S_62进制到10进制(this string ss) {
            int z进制 = 62;
            long ll = 0;
            byte[] bb = Encoding.UTF8.GetBytes(ss);
            for (int i = 0; i < bb.Length; i++) {
                if (bb[i] > 96) {
                    bb[i] = byte.Parse((bb[i] - 87).ToString());
                } else if (bb[i] > 64) {
                    bb[i] = byte.Parse((bb[i] - 29).ToString());
                } else {
                    bb[i] = byte.Parse((bb[i] - 48).ToString());
                }
                ll += bb[i] * (long)Math.Pow(z进制, bb.Length - i - 1);
            }
            return ll;
        }
        public static string S_10进制到36进制(this int z数字) {
            int z进制 = 36;
            byte[] bb = new byte[20];
            int i = 0;
            while (z数字 != 0) {
                i++;
                bb[bb.Length - i] = byte.Parse((z数字 % z进制).ToString());
                z数字 /= z进制;
            }
            //0=0,A=36,a=10
            //0=48,A=65,a=97
            string ss = "";
            for (int i2 = bb.Length - i; i2 < bb.Length; i2++) {
                ss += " " + bb[i2];
                if (bb[i2] > 9) {
                    bb[i2] = byte.Parse((bb[i2] + 87).ToString());
                } else {
                    bb[i2] = byte.Parse((bb[i2] + 48).ToString());
                }
            }
            return System.Text.Encoding.UTF8.GetString(bb, bb.Length - i, i);
        }
        public static int S_36进制到10进制(this string ss) {
            int z进制 = 36;
            int ll = 0;
            byte[] bb = Encoding.UTF8.GetBytes(ss);
            //0=0,A=36,a=10
            //0=48,A=65,a=97
            for (int i = 0; i < bb.Length; i++) {
                if (bb[i] > 96) {
                    bb[i] = byte.Parse((bb[i] - 87).ToString());
                } else {
                    bb[i] = byte.Parse((bb[i] - 48).ToString());
                }
                ll += bb[i] * (int)Math.Pow(z进制, bb.Length - i - 1);
            }
            return ll;
        }
        public static void S_解析表格数据(this string ss, Dictionary<int, string[]> z集合) {
            string[] sss = ss.Split('\r');
            for (int i = 0; i < sss.Length; i++) {
                try {
                    string[] sss2 = sss[i].Split('\t');
                    int z编号 = int.Parse(sss2[0]);
                    z集合.Add(z编号, sss2);
                } catch (Exception ex) {
                    C_Log.LogErrorFormat("解析出错_" + sss[i] + "\n_" + ex.S_GetInfo());
                }
            }
        }
    }
}
